package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatUserVO;
import com.zenithmind.chat.service.ChatMessageReadService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 消息已读记录管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "消息已读记录管理", description = "消息已读记录相关接口")
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/chat/message-read")
@RequiredArgsConstructor
public class ChatMessageReadController {

    private final ChatMessageReadService messageReadService;

    @Operation(summary = "查询消息已读用户列表")
    @GetMapping("/read-users")
    public Result<List<ChatUserVO>> getMessageReadUsers(
            @Parameter(description = "消息ID") @RequestParam String messageId) {
        
        List<ChatUserVO> result = messageReadService.getMessageReadUsers(messageId);
        
        return Result.success(result);
    }

    @Operation(summary = "查询消息未读用户列表")
    @GetMapping("/unread-users")
    public Result<List<ChatUserVO>> getMessageUnreadUsers(
            @Parameter(description = "消息ID") @RequestParam String messageId,
            @Parameter(description = "群组ID") @RequestParam String groupId) {
        
        List<ChatUserVO> result = messageReadService.getMessageUnreadUsers(messageId, groupId);
        
        return Result.success(result);
    }

    @Operation(summary = "标记消息为已读")
    @PostMapping("/mark-read")
    public Result<Boolean> markMessageAsRead(
            @Parameter(description = "消息ID") @RequestParam String messageId) {
        
        String userId = UserContext.getUserId();
        Boolean result = messageReadService.markMessageAsRead(messageId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量标记消息为已读")
    @PostMapping("/batch-mark-read")
    public Result<Boolean> batchMarkAsRead(
            @Parameter(description = "消息ID列表") @RequestBody List<String> messageIds) {
        
        String userId = UserContext.getUserId();
        Boolean result = messageReadService.batchMarkAsRead(messageIds, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "标记群组所有消息为已读")
    @PostMapping("/mark-group-read")
    public Result<Boolean> markGroupMessagesAsRead(
            @Parameter(description = "群组ID") @RequestParam String groupId) {
        
        String userId = UserContext.getUserId();
        Boolean result = messageReadService.markGroupMessagesAsRead(groupId, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "检查用户是否已读消息")
    @GetMapping("/check-read")
    public Result<Boolean> checkUserReadMessage(
            @Parameter(description = "消息ID") @RequestParam String messageId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        Boolean result = messageReadService.checkUserReadMessage(messageId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "查询用户在群组中的最后已读时间")
    @GetMapping("/last-read-time")
    public Result<LocalDateTime> getUserLastReadTime(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        LocalDateTime result = messageReadService.getUserLastReadTime(groupId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "统计消息已读数量")
    @GetMapping("/count-read")
    public Result<Integer> countMessageReadUsers(
            @Parameter(description = "消息ID") @RequestParam String messageId) {
        
        Integer result = messageReadService.countMessageReadUsers(messageId);
        
        return Result.success(result);
    }

    @Operation(summary = "统计用户未读消息数量")
    @GetMapping("/count-unread")
    public Result<Integer> countUserUnreadMessages(
            @Parameter(description = "群组ID") @RequestParam String groupId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId) {
        
        String targetUserId = userId != null ? userId : UserContext.getUserId();
        Integer result = messageReadService.countUserUnreadMessages(groupId, targetUserId);
        
        return Result.success(result);
    }

    @Operation(summary = "清理过期已读记录")
    @PostMapping("/clean-expired")
    public Result<Integer> cleanExpiredReadRecords(
            @Parameter(description = "保留天数") @RequestParam(defaultValue = "30") Integer days) {
        
        Integer result = messageReadService.cleanExpiredReadRecords(days);
        
        return Result.success(result);
    }

    @Operation(summary = "获取已读统计信息")
    @GetMapping("/statistics")
    public Result<Object> getReadStatistics(
            @Parameter(description = "消息ID") @RequestParam(required = false) String messageId,
            @Parameter(description = "群组ID") @RequestParam(required = false) String groupId,
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        
        Object result = messageReadService.getReadStatistics(messageId, groupId, startTime, endTime);
        
        return Result.success(result);
    }
}
